<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;

use App\Models\Rotary;
use App\Models\Article;

use Storage;

class HomeController extends Controller
{
    public function home(Request $request)
    {
        $data['rotary'] = Rotary::get(['name', 'image'])->map(function($rot){
            return [
                'name' => $rot->name,
                'image'=> Storage::disk(config('admin.upload.disk'))->url($rot->image),
            ];
        })->toArray();

        $data['articles'] = Article::latest()->take(3)->get(['id', 'title', 'article_cate_id']);

        $user = $request->user();

        if($user){
            $user_address = $request->user()->addresses()->OrderByUseTime()->first();

            $data['user_address'] = $user_address ? $user_address->only(['id', 'province', 'city', 'district', 'full_address', 'address']) : '';

            $data['user_points'] = [
                'user_points'   => $request->user()->points,
                'points_rank'   => $request->user()->points_rank,
                'order_count'   => $request->user()->orders()->completedStatus()->count()
            ];
        }else{
            $data['user_address'] = '';

            $data['user_points'] = [
                'user_points'   => 0,
                'points_rank'   => 0,
                'order_count'   => 0,
            ];
        }

        $data['status'] = 0;

        return response()->json($data);
    }
}
